package com.xh.petdogexpert.expert.service.imp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.JsonMappingException;
import com.xh.petdogexpert.base.BaseException;
import com.xh.petdogexpert.expert.dto.Disease;
import com.xh.petdogexpert.expert.dto.Symptom;
import com.xh.petdogexpert.expert.service.IDiseaseService;
import com.xh.petdogexpert.expert.service.IExpertService;
import com.xh.petdogexpert.expert.service.IJudgeLogService;
import com.xh.petdogexpert.expert.service.ISymptomService;

@Service
public class ExpertServiceImp implements IExpertService {

	@Autowired
	ISymptomService symptomService;

	@Autowired
	IDiseaseService diseaseService;

	@Autowired
	IJudgeLogService judgeLogService;

	@Autowired
	HttpSession session;

	/**
	 * 写入session
	 * 
	 * @author linxixin
	 * @date 2016年3月24日
	 */
	private void saveInSession(List<Disease> diseaseSumCf, List<String> symptomIds, List<String> uncheckList) {

		session.setAttribute("diseaseSumCf", diseaseSumCf);
		session.setAttribute("symptomIds", symptomIds);
		session.setAttribute("uncheckList", uncheckList);
		System.out.println(symptomIds.size() + "  dd");
	}

	void addSymptomFrequency(List<String> symptomIds) {
		symptomService.addSymptomFrequency(symptomIds);
	}

	public void clearSession() {

		session.setAttribute("diseaseSumCf", null);
		session.setAttribute("symptomIds", null);
		session.setAttribute("uncheckList", null);
	}

	/**
	 * 初步鉴定，发送一些症状然后再返回其他症状
	 * 
	 * @author linxixin
	 * @date 2016年3月24日
	 * @see com.xh.petdogexpert.expert.service.IExpertService#getFirstCheckRet(javax.servlet.http.HttpServletResponse,
	 *      java.util.List, java.lang.Integer)
	 */

	@Override
	public List<Symptom> getFirstCheckRet(List<String> symptomIds, List<String> uncheckList) {
		clearSession();
		return getSymptomBySymptom(symptomIds, uncheckList);
	}

	/**
	 * 通过症状获取疾病然后再获取症状
	 * 
	 * @author linxixin
	 * @date 2016年4月7日
	 */
	@SuppressWarnings("unchecked")
	public List<Symptom> getSymptomBySymptom(List<String> symptomIds, List<String> uncheckList) {

		List<String> oSymptomIds = (List<String>) session.getAttribute("symptomIds");

		oSymptomIds = exceptDuplicate(oSymptomIds, symptomIds);

		List<String> oUncheckList = (List<String>) session.getAttribute("uncheckList");
		oUncheckList = exceptDuplicate(oUncheckList, uncheckList);

		System.out.println("!check   " + oSymptomIds);
		System.out.println("!uncheck " + oUncheckList);
		List<Disease> diseaseSumCf = diseaseService.getDiseaseSumCf(oSymptomIds);
		System.out.println("disease " + diseaseSumCf);
		// 按sumcf的值从大到小排序
		Collections.sort(diseaseSumCf, (a, b) -> b.getSumCf().compareTo(a.getSumCf()));

		saveInSession(diseaseSumCf, oSymptomIds, oUncheckList);

		oSymptomIds.addAll(oUncheckList);

		List<Symptom> nSymptoms = symptomService.getSecondSymptoms(diseaseSumCf, oSymptomIds);

		Collections.sort(nSymptoms, (a, b) -> b.getCf().compareTo(a.getCf()));
		List<Symptom> nnSymptoms = new ArrayList<Symptom>();

		// 去除重复的症状名
		for (Symptom s : nSymptoms) {
			boolean ok = true;
			for (Symptom i : nnSymptoms) {
				if (i.getId() == s.getId()) {
					ok = false;
					break;
				}
			}
			if (ok) {
				nnSymptoms.add(s);
			}
		}
		System.out.println("res " + nnSymptoms);
		if (nnSymptoms.size() > 10) {
			return nnSymptoms.subList(0, 10);
		}
		return nnSymptoms;
	}

	/**
	 * 去重方法,两个list合并为一个,去除重复的
	 * 
	 * @author linxixin
	 * @date 2016年4月7日
	 */
	public List<String> exceptDuplicate(List<String> a, List<String> b) {
		System.out.println("ddddddd " + a + "   " + b);
		;

		if (a == null) {
			a = new ArrayList<String>();
			if (b != null)
				a.addAll(b);
			return a;
		}

		if (b == null)
			return a;

		a.removeIf((String s) -> {
			for (String i : b) {
				if (s.equals(i)) {
					return true;
				}
			}
			return false;
		});
		System.out.println("a " + a);
		;
		a.addAll(b);
		System.out.println("b " + b);
		;
		System.out.println("aa " + a);
		;
		return a;
	}

	/**
	 * 根据第二次的选择返回疾病的比例
	 * 
	 * @author linxixin
	 * @throws IOException
	 * @throws JsonMappingException
	 * @throws Exception
	 * @date 2016年3月24日
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Disease> getNextCheckRet(List<String> symptomIds, List<String> uncheckList)
			throws BaseException, Exception {

		addSymptomFrequency(symptomIds);

		List<String> oSymptomIds = (List<String>) session.getAttribute("symptomIds");

		List<String> oUncheckList = (List<String>) session.getAttribute("uncheckList");

		oSymptomIds = exceptDuplicate(oSymptomIds, symptomIds);
		oUncheckList = exceptDuplicate(oUncheckList, uncheckList);

		List<Disease> diseaseSumCf = diseaseService.getDiseaseSumCf(oSymptomIds);
		diseaseSumCf = diseaseSumCf.subList(0, 3);

		saveInSession(diseaseSumCf, oSymptomIds, oUncheckList);

		System.out.println("check   " + oSymptomIds);
		System.out.println("uncheck " + oUncheckList);
		System.out.println("disease " + diseaseSumCf);

		List<Disease> diseases = diseaseService.getDiseaseBaseInfo(diseaseSumCf);
		// 按sumcf的值从大到小排序
		Collections.sort(diseases, (b, a) -> a.getSumCf().compareTo(b.getSumCf()));
		return diseases;
	}

	/**
	 * 再次选择，直接从cookies里面获取之前选择的症状再次匹配
	 * 
	 * @author linxixin
	 * @date 2016年3月24日
	 * @see com.xh.petdogexpert.expert.service.IExpertService#choseAgain(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */

	public List<Symptom> choseAgain() throws Exception {

		return getSymptomBySymptom(null, null);
	}

	@SuppressWarnings("unchecked")
	public boolean finish() {

		List<Disease> diseaseSumCf = (List<Disease>) session.getAttribute("diseaseSumCf");
		List<String> symptomIds = (List<String>) session.getAttribute("symptomIds");
		addSymptomFrequency(symptomIds);
		System.out.println("  !!! " + diseaseSumCf.size() + "   " + symptomIds.size() + "  ");
		;
		if (symptomIds == null || symptomIds.size() < 1)
			return false;
		clearSession();
		return judgeLogService.log(diseaseSumCf, symptomIds);
	}
}
